import Vue from 'vue'
import VueRouter from 'vue-router'
import Reg from '@/views/Reg/Reg.vue'
import Login from '@/views/Login/Login.vue'
import Main from '@/views/Main/Main.vue'
import Home from '../views/Menus/Home/Home.vue'
import UserInfo from '../views/Menus/User/UserInfo.vue'
import UserAvatar from '../views/Menus/User/UserAvatar.vue'
import UserPwd from '../views/Menus/User/UserPwd.vue'
import ArtCate from '../views/Menus/Article/ArtCate.vue'
import ArtList from '../views/Menus/Article/ArtList.vue'
// 注册路由
Vue.use(VueRouter)

// 添加路由规则
const routes = [
  // 注册路由配置
 {
  path:'/reg',
  name:'reg',
  component:Reg
 },
//  登录路由配置
 {
  path:'/login',
  name:'login',
  component:Login
 },
//  首页路由配置
 {
  path:'/',
  name:'main',
  component:Main,
  redirect:'/home', //重定向到主页
  children:[
    // 二级路由的嵌套的使用, 二级路由path 不用加斜杠
    {
      path:'home',
      name:'home',
      component:Home,
    },
    // 基本资料配置
    {
      path:'user-info',
      name:'userInfo',
      component:UserInfo,
    },
    // 更换头像配置
    {
      path:'user-avatar',
      name:'userAvatar',
      component:UserAvatar,
    },
    // 修改密码配置
    {
      path:'user-pwd',
      name:'userPwd',
      component:UserPwd,
    },
    // 文章管理配置
    {
      path:'art-cate',
      name:'artCate',
      component:ArtCate,
    },
    // 文章分类配置
    {
      path:'art-list',
      name:'artList',
      component:ArtList,
    },
  ]
 },
]
// 创建路由实例
const router = new VueRouter({
  routes
})

// VueRouter.prototype.push: push函数要求不能重复 跳转路由
// 解决方法, 就是改写 VueRouter中的push函数, 但需要保留之前的功能
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
 return originalPush.call(this, location).catch(err => err)
}



// 暴露路由实例
export default router
